/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-06
 */
package org.jeecg.modules.flow.callback.delivery.in;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.delivery.util.CommonDeliveryTaskUtil;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.enums.InboundStatusEnum;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.callback.inbound.order.InBoundBeanUtils;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实现任务节点状态处理
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-06
 */
@RequiredArgsConstructor
@Component
@Slf4j
public class R105InBoundTaskDBRKInBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsInReceiptTaskService iWmsInReceiptTaskService;

    private final InBoundBeanUtils inBoundBeanUtils;

    private final CommonDeliveryTaskUtil commonDeliveryTaskUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        log.info("任务回调执行了");
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId= baseFlowDto.getInfoId();
        WmsInReceiptTask po = iWmsInReceiptTaskService.getById(taskId);
        if (InboundStatusEnum.PART.getCode().equals(po.getWarehouseStatus())){
            po.setDocumentStatus(DocumentStatusEnum.PROCESS.getCode());
        }
        if (InboundStatusEnum.ALL.getCode().equals(po.getWarehouseStatus())){
            po.setDocumentStatus(DocumentStatusEnum.COMPLETE.getCode());
            //通知mes任务结束
            inBoundBeanUtils.sendMesCompleted(po.getId());
        }
        iWmsInReceiptTaskService.updateById(po);
        if (DocumentStatusEnum.COMPLETE.getCode().equals(po.getDocumentStatus())){
            baseFlowDto.setRunThenStop(false);
            commonDeliveryTaskUtil.updateMaterialDeliveryStatus(SimpleDocumentTypeEnum.IN_TASK,taskId,null,"已完成",null);
            // 推动调拨任务流程节点
            commonDeliveryTaskUtil.pushInFlow(taskId);
        }else {
            //true 不更新节点
            baseFlowDto.setRunThenStop(true);
        }
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
        baseFlowDto.setResult(Result.OK());
    }
}
